#ifndef __ADC_H
#define __ADC_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "system_remap.h"

typedef struct
{
	uint32_t ADC_SampleTime;
  uint32_t ADC_ExternalTrigConv;          /*!< Defines the external trigger used to start the analog
                                               to digital conversion of channels. This parameter
                                               can be a value of @ref ADC_external_trigger_sources_for_channels_conversion */
  uint8_t ADC_Channel;               /*!< Specifies  ADC channels that will be converted   This parameter must range from 0 to 28. */
	uint8_t ADC_ChannelNum;							/*multichannel num*/
	uint8_t ADC_ScanMode;							/*singlechannel or multichannel*/
		
}ADC_InitTypeDef;
/**
  * @}
  */



/** @defgroup ADC_ClockDivider
  * @{
  */

#define ADC_CLK_PCLK_DIV_2					((uint32_t)0x00)
#define ADC_CLK_PCLK_DIV_4					((uint32_t)0x01)
#define ADC_CLK_PCLK_DIV_6					((uint32_t)0x02)
#define ADC_CLK_PCLK_DIV_8					((uint32_t)0x03)
#define ADC_CLK_PCLK_DIV_10					((uint32_t)0x04)
#define ADC_CLK_PCLK_DIV_12					((uint32_t)0x05)
#define ADC_CLK_PCLK_DIV_14					((uint32_t)0x06)
#define ADC_CLK_PCLK_DIV_16					((uint32_t)0x07)
#define ADC_CLK_PCLK_DIV_18					((uint32_t)0x08)
#define ADC_CLK_PCLK_DIV_20					((uint32_t)0x09)
#define ADC_CLK_PCLK_DIV_22					((uint32_t)0x0A)
#define ADC_CLK_PCLK_DIV_24					((uint32_t)0x0B)
#define ADC_CLK_PCLK_DIV_26					((uint32_t)0x0C)
#define ADC_CLK_PCLK_DIV_28					((uint32_t)0x0D)
#define ADC_CLK_PCLK_DIV_30					((uint32_t)0x0E)
#define ADC_CLK_PCLK_DIV_32					((uint32_t)0x0F)
#define ADC_CLK_PCLK_DIV_34					((uint32_t)0x10)
#define ADC_CLK_PCLK_DIV_36					((uint32_t)0x11)
#define ADC_CLK_PCLK_DIV_38					((uint32_t)0x12)
#define ADC_CLK_PCLK_DIV_40					((uint32_t)0x13)
#define ADC_CLK_PCLK_DIV_42					((uint32_t)0x14)
#define ADC_CLK_PCLK_DIV_44					((uint32_t)0x15)
#define ADC_CLK_PCLK_DIV_46					((uint32_t)0x16)
#define ADC_CLK_PCLK_DIV_48					((uint32_t)0x17)
#define ADC_CLK_PCLK_DIV_50					((uint32_t)0x18)
#define ADC_CLK_PCLK_DIV_52					((uint32_t)0x19)
#define ADC_CLK_PCLK_DIV_54					((uint32_t)0x1A)
#define ADC_CLK_PCLK_DIV_56					((uint32_t)0x1B)
#define ADC_CLK_PCLK_DIV_58					((uint32_t)0x1C)
#define ADC_CLK_PCLK_DIV_60					((uint32_t)0x1D)
#define ADC_CLK_PCLK_DIV_62					((uint32_t)0x1E)
#define ADC_CLK_PCLK_DIV_64					((uint32_t)0x1F)
#define ADC_CLK_PCLK_DIV_66					((uint32_t)0x20)
#define ADC_CLK_PCLK_DIV_68					((uint32_t)0x21)
#define ADC_CLK_PCLK_DIV_70					((uint32_t)0x22)
#define ADC_CLK_PCLK_DIV_72					((uint32_t)0x23)
#define ADC_CLK_PCLK_DIV_74					((uint32_t)0x24)
#define ADC_CLK_PCLK_DIV_76					((uint32_t)0x25)
#define ADC_CLK_PCLK_DIV_78					((uint32_t)0x26)
#define ADC_CLK_PCLK_DIV_80					((uint32_t)0x27)
#define ADC_CLK_PCLK_DIV_82					((uint32_t)0x28)
#define ADC_CLK_PCLK_DIV_84					((uint32_t)0x29)
#define ADC_CLK_PCLK_DIV_86					((uint32_t)0x2A)
#define ADC_CLK_PCLK_DIV_88					((uint32_t)0x2B)
#define ADC_CLK_PCLK_DIV_90					((uint32_t)0x2C)
#define ADC_CLK_PCLK_DIV_92					((uint32_t)0x2D)
#define ADC_CLK_PCLK_DIV_94					((uint32_t)0x2E)
#define ADC_CLK_PCLK_DIV_96					((uint32_t)0x2F)
#define ADC_CLK_PCLK_DIV_98					((uint32_t)0x30)
#define ADC_CLK_PCLK_DIV_100					((uint32_t)0x31)
#define ADC_CLK_PCLK_DIV_102					((uint32_t)0x32)
#define ADC_CLK_PCLK_DIV_104					((uint32_t)0x33)
#define ADC_CLK_PCLK_DIV_106					((uint32_t)0x34)
#define ADC_CLK_PCLK_DIV_108					((uint32_t)0x35)
#define ADC_CLK_PCLK_DIV_110					((uint32_t)0x36)
#define ADC_CLK_PCLK_DIV_112					((uint32_t)0x37)
#define ADC_CLK_PCLK_DIV_114					((uint32_t)0x38)
#define ADC_CLK_PCLK_DIV_116					((uint32_t)0x39)
#define ADC_CLK_PCLK_DIV_118					((uint32_t)0x3A)
#define ADC_CLK_PCLK_DIV_120					((uint32_t)0x3B)
#define ADC_CLK_PCLK_DIV_122					((uint32_t)0x3C)
#define ADC_CLK_PCLK_DIV_124					((uint32_t)0x3D)
#define ADC_CLK_PCLK_DIV_126					((uint32_t)0x3E)
#define ADC_CLK_PCLK_DIV_128					((uint32_t)0x3F)





#define ADC_REF_VOLTAGE_INN1_5				((uint16_t)0x00)
#define ADC_REF_VOLTAGE_VDD						((uint16_t)0x01)
#define ADC_REF_VOLTAGE_EXT						((uint16_t)0x02)
#define ADC_REF_VOLTAGE_INN						((uint16_t)0x03)



/** @defgroup ADC_mode 
  * @{
  */
#define ADC_ScanMode_SingleChannel		((uint16_t)0x00)
#define ADC_ScanMode_MultiChannel			((uint16_t)0x01)



#define ADC_NumChannel_1					((uint16_t)0x00)
#define ADC_NumChannel_2					((uint16_t)0x01)
#define ADC_NumChannel_3					((uint16_t)0x02)
#define ADC_NumChannel_4					((uint16_t)0x03)
#define ADC_NumChannel_5					((uint16_t)0x04)
#define ADC_NumChannel_6					((uint16_t)0x05)

/**
  * @}
  */

/** @defgroup ADC_external_trigger_sources_for_regular_channels_conversion 
  * @{
  */


#define ADC_ExternalTrigConv_None              	((uint32_t)0x00) /*for ADC*/
#define ADC_ExternalTrigConv_T1_CH4_H           ((uint32_t)0x01) /*for ADC*/
#define ADC_ExternalTrigConv_T14_CH1_H          ((uint32_t)0x02) /*for ADC*/
#define ADC_ExternalTrigConv_T1_CH3_H           ((uint32_t)0x03) /*for ADC*/
#define ADC_ExternalTrigConv_T1_CH2_H          	((uint32_t)0x04) /*for ADC*/
#define ADC_ExternalTrigConv_T1_CH1_H          	((uint32_t)0x05) /*for ADC*/
#define ADC_ExternalTrigConv_EXTI_4_H         	((uint32_t)0x06) /*for ADC*/
#define ADC_ExternalTrigConv_EXTI_15_H          ((uint32_t)0x07) /*for ADC*/


/**
  * @}
  */

/** @defgroup AMP GAIN 
  * @{
  */

#define AMP_Gain_2				((uint8_t)0x00)
#define AMP_Gain_4        ((uint8_t)0x01)
#define AMP_Gain_8        ((uint8_t)0x02)
#define AMP_Gain_16       ((uint8_t)0x03)
#define AMP_Gain_32       ((uint8_t)0x04)
                          
/**                       
  * @}                    
  */

/** @defgroup ADC_channels 
  * @{
  */

#define ADC_Channel_0                               ((uint8_t)0x01)
#define ADC_Channel_1                               ((uint8_t)0x02)
#define ADC_Channel_2                               ((uint8_t)0x03)
#define ADC_Channel_3                               ((uint8_t)0x04)
#define ADC_Channel_4                               ((uint8_t)0x05)
#define ADC_Channel_5                               ((uint8_t)0x06)
#define ADC_Channel_6                               ((uint8_t)0x07)
#define ADC_Channel_7                               ((uint8_t)0x08)
#define ADC_Channel_8                               ((uint8_t)0x09)
#define ADC_Channel_9                               ((uint8_t)0x0A)
#define ADC_Channel_10                              ((uint8_t)0x0B)
#define ADC_Channel_11                              ((uint8_t)0x0C)
#define ADC_Channel_12                              ((uint8_t)0x0D)
#define ADC_Channel_13                              ((uint8_t)0x0E)
#define ADC_Channel_14                              ((uint8_t)0x0F)
#define ADC_Channel_15                              ((uint8_t)0x10)
#define ADC_Channel_16                              ((uint8_t)0x11)
#define ADC_Channel_17                              ((uint8_t)0x12)
#define ADC_Channel_18                              ((uint8_t)0x13)
#define ADC_Channel_19                              ((uint8_t)0x14)
#define ADC_Channel_20                              ((uint8_t)0x15)
#define ADC_Channel_21                              ((uint8_t)0x16)
#define ADC_Channel_22                              ((uint8_t)0x17)
#define ADC_Channel_23                              ((uint8_t)0x18)
#define ADC_Channel_24                              ((uint8_t)0x19)
#define ADC_Channel_25                              ((uint8_t)0x1A)
#define ADC_Channel_26                              ((uint8_t)0x1B)
#define ADC_Channel_27                              ((uint8_t)0x1C)
#define ADC_Channel_28                              ((uint8_t)0x1D)	//VDD/4

#define ADC_Channel_VDD_1V4                         ((uint8_t)0x1D)

/** @defgroup ADC_sampling_time 
  * @{
  */


#define ADC_SampleTime_1Cycles                    ((uint8_t)0x00)
#define ADC_SampleTime_2Cycles                    ((uint8_t)0x01)
#define ADC_SampleTime_4Cycles                    ((uint8_t)0x02)
#define ADC_SampleTime_8Cycles                    ((uint8_t)0x03)
#define ADC_SampleTime_16Cycles                   ((uint8_t)0x04)
#define ADC_SampleTime_32Cycles                   ((uint8_t)0x05)
#define ADC_SampleTime_64Cycles                   ((uint8_t)0x06)
#define ADC_SampleTime_128Cycles                  ((uint8_t)0x07)



/**
  * @}
  */

/** @defgroup ADC_interrupts_definition 
  * @{
  */

#define ADC_IT_EOC                                 ((uint16_t)0x0080)
#define ADC_IT_AWD                                 ((uint16_t)0x4000)




/** @defgroup ADC_flags_definition 
  * @{
  */

#define ADC_FLAG_AWD                               ((uint8_t)0x4000)
#define ADC_FLAG_EOC                               ((uint8_t)0x0080)


/************************ADC CFG**************************************/

#define ADC_CFG_Vref_Mask											 		  ((uint16_t)0x0003)

#define ADC_CFG_SampleTim_Mask										  ((uint16_t)0x001C)

#define ADC_CFG_CONV_Start													((uint16_t)0x0020)	

#define ADC_CFG_IT_Set															((uint16_t)0x0040)

#define ADC_CFG_Complete_Set												((uint16_t)0x0080)

#define ADC_CFG_CHx_Mask														((uint16_t)0x1F00)

#define ADC_CFG_Compare_Set													((uint16_t)0x2000)

#define ADC_CFG_Overflow_Mask												((uint16_t)0x4000)

#define ADC_CFG_DMA_Set															((uint16_t)0x8000)

/********************************************************************/

/************************ADC CPDL/CPDH********************************/
#define ADC_CPDL_Mask																((uint16_t)0xFFF0)
#define ADC_CPDH_Mask																((uint16_t)0xFFF0)

/*********************************************************************/
/************************ADC DA********************************/
#define ADC_Data_Mask																((uint16_t)0xFFF0)
/*********************************************************************/


/************************ADC CTRL************************************/
#define ADC_CTRL_ConvTrig_Mask											((uint16_t)0x0007)

#define ADC_CTRL_OnOff_Set													((uint16_t)0x0008)

#define ADC_CTRL_CLKDIV_Mask												((uint16_t)0x03F0)

#define ADC_CTRL_AMP_Set														((uint16_t)0x0400)

#define ADC_CTRL_AMPGain_Mask												((uint16_t)0x3800)

#define ADC_CTRL_ADCCAL_Set													((uint16_t)0x4000)

/*********************************************************************/

/************************ADC SEQ/CHSEL********************************/
#define ADC_SEQ_Set																	((uint16_t)0x0001)
#define ADC_SEQ_Num_Mask														((uint16_t)0x000E)
#define ADC_SEQ_CH1SEL_Mask													((uint16_t)0x03E0)
#define ADC_SEQ_CH2SEL_Mask													((uint16_t)0x7C00)

#define ADC_CHSEL_CH3SEL_Mask												((uint16_t)0x001F)
#define ADC_CHSEL_CH4SEL_Mask												((uint16_t)0x03E0)
#define ADC_CHSEL_CH5SEL_Mask												((uint16_t)0x7C00)

/*********************************************************************/

/************************ADC SEQ/CHSEL********************************/
#define ADC_DATAT0_Mask															((uint16_t)0xFFF0)
#define ADC_DATAT1_Mask															((uint16_t)0xFFF0)
#define ADC_DATAT2_Mask															((uint16_t)0xFFF0)
#define ADC_DATAT3_Mask															((uint16_t)0xFFF0)
#define ADC_DATAT4_Mask															((uint16_t)0xFFF0)
#define ADC_DATAT5_Mask															((uint16_t)0xFFF0)
/*********************************************************************/

/** @defgroup ADC_Exported_Functions
  * @{
  */

void ADC_DeInit(ADC_TypeDef* ADCx);
void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct);
void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct);
void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState);
void ADC_SingleChannelStart(ADC_TypeDef* ADCx);
void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState);
void ADC_ITConfig(ADC_TypeDef* ADCx,FunctionalState NewState);
FlagStatus ADC_GetConvStatus(ADC_TypeDef* ADCx);
void ADC_ExternalTrigConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
uint16_t ADC_GetSingleChannelConversionValue(ADC_TypeDef* ADCx);

void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
void ADC_AnalogWatchdogConfigMax(ADC_TypeDef* ADCx, uint16_t ADC_Value);
void ADC_AnalogWatchdogConfigMin(ADC_TypeDef* ADCx, uint16_t ADC_Value);


FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint16_t ADC_FLAG);
void ADC_ClearFlag(ADC_TypeDef* ADCx, uint16_t ADC_FLAG);
ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT);
void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT);
void ADC_CLKConfig(ADC_TypeDef* ADCx,uint16_t ADC_ClockDivider);
void ADC_REFConfig(ADC_TypeDef* ADCx,uint16_t ADC_RefVoltage);
void ADC_ChannelSet(ADC_TypeDef* ADCx,uint8_t Channel);

void ADC_MultiChannelScanCmd(ADC_TypeDef* ADCx,FunctionalState NewState);
void ADC_MultiChannel1Config(ADC_TypeDef* ADCx,uint8_t Channel);
void ADC_MultiChannel2Config(ADC_TypeDef* ADCx,uint8_t Channel);
void ADC_MultiChannel3Config(ADC_TypeDef* ADCx,uint8_t Channel);
void ADC_MultiChannel4Config(ADC_TypeDef* ADCx,uint8_t Channel);
void ADC_MultiChannel5Config(ADC_TypeDef* ADCx,uint8_t Channel);
void ADC_MultiChannel6Config(ADC_TypeDef* ADCx,uint8_t Channel);
void ADC_MultiChannelStart(ADC_TypeDef* ADCx,uint8_t FirstChannel);
uint16_t ADC_GetMultiChannelxConversionValue(ADC_TypeDef* ADCx,uint8_t ADC_MultiNumChannel_x);

void ADC_AMPCmd(ADC_TypeDef* ADCx,FunctionalState NewState);
void ADC_AMPGainSet(ADC_TypeDef* ADCx,uint8_t Gain);


#ifdef __cplusplus
}
#endif

#endif /*__ADC_H */
